package com.shijie.newvideo.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

/**
 * @author zhangdunxiang
 */

public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<BaseRecyclerViewHolder<T>> {

    protected Context mContext;

    protected List<T> mData;

    private BaseRecyclerViewHolder.OnItemClickListener mOnItemClickListener;

    public BaseRecyclerViewAdapter(Context context, List<T> data) {
        mContext = context;
        mData = data;
    }

    @Override
    public int getItemCount() {
        if (mData == null) {
            return 0;
        } else {
            return mData.size();
        }
    }

    /**
     * 通过{@linkplain #getItemLayoutRes(int)}获取布局id并加载出itemView，
     * 同时{@linkplain BaseRecyclerViewHolder#setOnItemClickListener(BaseRecyclerViewHolder.OnItemClickListener) 设置itemClick}
     *
     * @param parent parent
     * @param viewType 类型
     * @return view holder
     */
    @Override
    public BaseRecyclerViewHolder<T> onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View itemView = layoutInflater.inflate(getItemLayoutRes(viewType), parent, false);
        BaseRecyclerViewHolder<T> viewHolder = createViewHolderWithViewType(itemView, viewType);
        if (mOnItemClickListener == null) {
            mOnItemClickListener = getDefaultOnItemClickListener();
        }
        if (mOnItemClickListener != null) {
            viewHolder.setOnItemClickListener(mOnItemClickListener);
        }
        return viewHolder;
    }

    protected BaseRecyclerViewHolder.OnItemClickListener getDefaultOnItemClickListener() {
        return null;
    }

    protected BaseRecyclerViewHolder<T> createViewHolderWithViewType(View itemView, int viewType) {
        return createViewHolder(itemView);
    }

    /**
     * 创建一个ViewHolder，一般直接new就可以了
     */
    protected abstract BaseRecyclerViewHolder<T> createViewHolder(View itemView);

    /**
     * item的布局id，可以用viewType获取不同的布局，但别忘了{@linkplain #getItemCount()}和{@linkplain #getItemViewType(int)}
     */
    protected abstract int getItemLayoutRes(int viewType);

    /**
     * 调用的{@linkplain BaseRecyclerViewHolder#bindTo(Object, int)}
     */
    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder<T> holder, int position) {
        if (mData != null && position < mData.size()) {
            holder.bindTo(mData.get(position), position);
        }
    }

    public void setOnItemClickListener(BaseRecyclerViewHolder.OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public List<T> getData() {
        return mData;
    }

    /**
     * 去掉一条数据
     */
    protected void removeListItem(T t) {
        int position = mData.indexOf(t);
        if (position >= 0){
            mData.remove(position);
            this.notifyItemRemoved(position);
        }
    }

    /**
     * 插入一条数据
     *
     * @param t 数据
     * @param position 插入位置
     */
    protected void addItem(T t, int position) {
        mData.add(position, t);
        notifyItemInserted(position);
    }

    /**
     * 插入一组数据
     *
     * @param list 插入一组数据
     * @param position 插入位置
     */
    protected void addListItem(List<T> list, int position) {
        mData.addAll(position, list);
        notifyItemRangeInserted(position, list.size());
    }
}
